package com.shangcheng.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.SmsFlashPromotionProductRelationMapper;
import com.shangcheng.model.domain.SmsFlashPromotionProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
public class SmsFlashPromotionProductRelationApiImpl implements SmsFlashPromotionProductRelationApi {
    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    /**
     * 根据flashPromotionId进行查询商品个数
     *
     * @param flashPromotionSession
     * @return
     */
    @Override
    public Integer findAllProductCountByFlashCondition(Integer flashPromotionId,Integer flashPromotionSession) {
        //设置查询条件
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSession)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        Integer count = smsFlashPromotionProductRelationMapper.selectCount(wrapper);
        return count;
    }

    /**
     * 添加活动和产品关系
     *
     * @param relationList
     */
    @Override
    @Transactional
    public void save(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : relationList) {
            smsFlashPromotionProductRelationMapper.insert(smsFlashPromotionProductRelation);
        }
    }

    /**
     * 删除活动和产品关系
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        smsFlashPromotionProductRelationMapper.deleteById(id);
    }

    /**
     * 修改活动和产品关系
     *
     * @param relation
     */
    @Override
    public void update(SmsFlashPromotionProductRelation relation) {
        smsFlashPromotionProductRelationMapper.updateById(relation);
    }

    /**
     * 通过id查找活动和产品关系
     *
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotionProductRelation findById(Integer id) {
        return smsFlashPromotionProductRelationMapper.selectById(id);
    }

    /**
     * 分页和加条件查找数据
     *
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage findList(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        IPage page = new Page(pageNum, pageSize);

        LambdaQueryWrapper<SmsFlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId).eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId)
                .orderByDesc(SmsFlashPromotionProductRelation::getSort);

        smsFlashPromotionProductRelationMapper.selectPage(page, wrapper);

        return page;
    }
}
